/**
 * Advanced Software Engineering (Monica Farrow)
 * ParcelGUI class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depotViews;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import depotInterfaces.*;
import depotModels.ParcelUncollected;
import depot.*;

/**
 * ParcelGUI class - displays data from ParcelUncollected subject
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class ParcelGUI extends JFrame implements DepotObserver {

	private static final long serialVersionUID = 1L;

	private ParcelUncollected model;        // holds parcels awaiting processing

	// Title panel
	JPanel titlePanel = new JPanel();
	JLabel guiTitle = new JLabel();         // GUI heading

	// GUI componenets
	JPanel pclsPanel = new JPanel();
	Object[][] pclsRows = null;                     // holds details of processed customers
	JLabel totalPcls = new JLabel();                // holds total no. processed customers
	JTable pclsTable = new JTable();                // table of processed customers
	DefaultTableModel pclsTabModel = new DefaultTableModel(); // table model for pclsTable

	// holds column headers 
	String[] columns = {"Parcel ID", "Dimensions", "Weight", "Days"};


	/**
	 * constructor for ParcelGUI
	 * registers ParcelGUI with ParcelUncollected object (model)
	 * contained in the parameter and creates GUI display
	 * @param model  the ParcelUncollected object
	 */
	public ParcelGUI(ParcelUncollected model) {
		this.model = model;

		// register with the model
		model.registerObserver(this);

		// Create the table of parcels
		createDisplay();

	}


	/**
	 * creates and starts the GUI
	 */
	public void createDisplay() {

		// set GUI titlebar
		this.setTitle("Parcel List");

		// Sets location of GUI
		this.setLocation(120,150);

		// Set size of GUI
		this.setMinimumSize(new Dimension(545, 145));
		
		// disable GUI close button
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

		// set layout
		this.setLayout(new BorderLayout());

		// create & add title panel
		createTitlePanel();
		this.add(titlePanel, BorderLayout.NORTH);

		// create & add current customer panel
		createPclsPanel();
		this.add(pclsPanel, BorderLayout.CENTER);

		// Pack and make GUI visible
		this.pack();
		this.setVisible(true);
	}


	/**
	 *  Creates the title panel
	 */
	public void createTitlePanel() {

		titlePanel.setLayout(new FlowLayout());

		// create GUI header
		guiTitle.setText(" Parcel List ");

		// set font large and bold 
		guiTitle.setFont(new Font (Font.SANS_SERIF, Font.BOLD,14));

		// add light border
		guiTitle.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));

		guiTitle.setOpaque(true);

		// set background colour to white
		guiTitle.setBackground(Color.WHITE);  

		// add it at the top of the title panel
		titlePanel.add(guiTitle);                               
	}


	/**
	 *  Creates the parcel list panel
	 */
	public void createPclsPanel() {

		pclsPanel.setLayout(new BorderLayout());
		pclsPanel.setSize(550, 145);

		// create table and make un-editiable
		pclsTable = new JTable(new DefaultTableModel(pclsRows, columns))
		{               
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rows, int columns)
			{
				return false;
			}
		};

		// set table model (allows insert/delete rows)
		pclsTabModel = (DefaultTableModel) pclsTable.getModel();

		// Align all the cell to center view
		DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
		cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
		pclsTable.getColumn("Parcel ID").setCellRenderer( cellRenderer );
		pclsTable.getColumn("Dimensions").setCellRenderer( cellRenderer );
		pclsTable.getColumn("Weight").setCellRenderer( cellRenderer );
		pclsTable.getColumn("Days").setCellRenderer( cellRenderer );

		// Size of scrollable area
		pclsTable.setPreferredScrollableViewportSize(new Dimension(500,100));
		pclsTable.setFillsViewportHeight(false);

		JScrollPane pclsContainer = new JScrollPane(pclsTable);


		// add components to panel
		pclsPanel.add(pclsContainer, BorderLayout.NORTH);

	} 

	/**
	 * updates the data in the GUI display
	 * by fetching it from the model
	 * @param info   information about the data change
	 */
	public synchronized void update(String info) {

		// take info from model and put into array
		String type [] = info.split(" ");
		Parcel pcl;

		// if a parcel has been added to parcelList
		if(type[0].equalsIgnoreCase("add")) {

			// get parcelID passed from model
			pcl = model.getPcl(type[1]); 

			// add parcel details in a new row (end of table)
			pclsTabModel.addRow(new Object[]{pcl.getParcelID(), pcl.getDimensionsAsString(), pcl.getWeight(), pcl.getDays()});

		}

		// if a parcel has been removed from parcelList
		else if(type[0].equalsIgnoreCase("rem")) {

			// loop through table until locate parcelID
			for(int row = 0; row < pclsTabModel.getRowCount(); row++) {
				for(int col = 0 ;col < pclsTabModel.getColumnCount(); col++) {
					if(pclsTabModel.getValueAt(row, col).toString().equalsIgnoreCase(type[1])) {
						pclsTabModel.removeRow(row); // Delete the parcel from the table
						return;
					}
				}
			}
		}
	}       

}